package com.eight.system.service.impl;

import cn.hutool.core.util.IdUtil;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eight.common.pojo.AclRole;
import com.eight.common.pojo.AclUserRole;
import com.eight.common.vo.Result;
import com.eight.system.dao.AclRoleDao;
import com.eight.system.dao.UserRoleDao;
import com.eight.system.service.RoleService;
import com.eight.system.vo.RoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.management.relation.Role;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/24 20:30
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private AclRoleDao roleDao;

    @Autowired
    private UserRoleDao userRoleDao;
    @Override
    public List<AclRole> findAll() {
        return roleDao.selectList(null);
    }

    @Override
    public List<AclRole> findByUserId(String userId) {
        List<AclRole>  roles=roleDao.selectByUserId(userId);
        return roles;
    }

    @Override
    public Integer confirm(String userId, String[] roleIds) {
        //删除该用户具有的角色
        roleDao.deleteByUserId(userId);
        //批量添加用户角色
        List<AclUserRole> userRoleList = Arrays.stream(roleIds).map(roleId -> new AclUserRole(IdUtil.getSnowflakeNextId()+"", roleId, userId, 0, LocalDateTime.now(), LocalDateTime.now())).collect(Collectors.toList());
        userRoleDao.batchInsert(userRoleList);
        return null;
    }
    @Override
    public Page<AclRole> findPage(Integer currentSize, Integer pageSize, RoleVo roleVo) {
        Page<AclRole> page=new Page<>(currentSize,pageSize);
        QueryWrapper<AclRole> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if(roleVo.getIsDeleted()!=null){
            wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }
        Page<AclRole> page1 = roleDao.selectPage(page,wrapper);
        return page1;
    }

    @Override
    public int updateRoleStatusByid(String id, Integer status) {
        AclRole role = new AclRole();
        role.setId(id);
        role.setIsDeleted(status);
        role.setGmtModified(LocalDateTime.now());
        int updateById = roleDao.updateById(role);
        return updateById;
    }


    @Override
    public int deleteRoleById(String id) {
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if (id != null){
            wrapper.eq("id",id);
        }
        int delete = roleDao.delete(wrapper);
        return delete;
    }

    @Override
    public int addRoleByRle(AclRole aclRole) {
        aclRole.setId(IdUtil.getSnowflakeNextIdStr()+"");
        aclRole.setGmtCreate(LocalDateTime.now());
        aclRole.setGmtModified(LocalDateTime.now());
        aclRole.setIsDeleted(0);
        aclRole.setRoleCode("1");
        int insert = roleDao.insert(aclRole);
        return insert;
    }

    @Override
    public int updateRoleByRole(AclRole aclRole) {
        aclRole.setGmtModified(LocalDateTime.now());
        int i = roleDao.updateById(aclRole);
        return i;
    }
}
